<template>
  <el-dialog title="分配角色" :visible="isShow" width="30%">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkList">
      <!-- 选项 -->
      <el-checkbox v-for="item in allRoleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="isShow = false">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRolesList } from '@/api/settings'
import { getEmployeeInfoData, assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isShow: false,
      allRoleList: [],
      checkList: [],
      userId: null
    }
  },
  methods: {
    // 获取所有角色
    async showRoleList() {
      const { data } = await getRolesList({
        page: 1,
        pagesize: 100
      })
      this.allRoleList = data.rows
      this.isShow = true
    },
    // 获取当前用户的所有信息
    async getUserRole(id) {
      const { data } = await getEmployeeInfoData(id)
      this.checkList = data.roleIds
      this.userId = id
    },
    async btnOK() {
      await assignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('关联成功')
      this.isShow = false
    }
  }
}
</script>
